﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Document.Library;
using Container.Library;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Staff.Controllers
{
    /// <summary>
    /// 员工工资发放控制器
    /// </summary>
    [Area("Staff")]
    public class SalaryPaymentController : BaseController
    {
        private IHostingEnvironment hostingEnv;
        private readonly IEmployeeService employeeService = null;//员工管理逻辑

        public SalaryPaymentController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
            //员工管理逻辑注入
            employeeService = UnityCIContainer.Instance.GetService<IEmployeeService>();
        }

        #region 界面

        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 修改补贴界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Subsidy()
        {
            return View();
        }

        #endregion

        #region 查询

        /// <summary>
        /// 获取工资单分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadEmployeeSalaryPaymentList()
        {
            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<SalaryPaymentQueryRequest>>();

                resultInfo = employeeService.LoadEmployeeSalaryPaymentList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <returns>处理结果</returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult EpxortSalaryPayment()
        {
            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();

            FileStream fs = null;
            BinaryWriter bw = null;

            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<SalaryPaymentQueryRequest>>();

                resultInfo = employeeService.LoadEmployeeSalaryPaymentAllList(parametersInfo);

                List<string> headlist = new List<string>()
                {
                   "姓名","性别","工种","工资单号","年份","月份","借款金额",
                   "实发工资","考勤天数","加班小时","加班工资","计件工资",
                   "调补天数","调补工资","交通","电话","奖金","其他1","其他2","支付方式",
                   "身份证号","银行账号","备注"
                };

                string fileName = string.Empty;
                switch (parametersInfo.parameters.iType)
                {
                    case 11:
                        fileName = "员工工资发放信息-未发放" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                        break;
                    case 22:
                        fileName = "员工工资发放信息-已发放" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                        break;
                    case 33:
                        fileName = "员工工资发放信息-有异常" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                        break;
                }

                var savePath = ExcelHelper.ExportToExcelReturnFilePath<T_SalaryCountEntity>(resultInfo.Data, headlist, rowData =>
                {
                    return new object[] {
                                    rowData.Name,
                                    rowData.Sex,
                                    rowData.WorkTypeName,
                                    rowData.FNO,
                                    rowData.MyYear,
                                    rowData.MyMonth,
                                    rowData.LoanMoney,
                                    rowData.RealMoney,
                                    rowData.WorkDay,
                                    rowData.OverTime,
                                    rowData.TOverMoney,
                                    rowData.AmountMoney,
                                    rowData.RepairDays,
                                    rowData.RepairMoney,
                                    rowData.CarBT,
                                    rowData.PhoneBT,
                                    rowData.BonusBT,
                                    rowData.OtherBT1,
                                    rowData.OtherBT2,
                                    rowData.PayMethodName,
                                    rowData.Code,
                                    rowData.BanKNo,
                                    rowData.Rmark
                           };
                },
                false,
                hostingEnv.WebRootPath,
                fileName
                );
                resultInfo.Msg = savePath;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            finally
            {
                if (fs != null) fs.Close();
                if (bw != null) bw.Close();
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 工资发放操作

        /// <summary>
        ///修改补助
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult SaveSubsidy()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<SalarySubsidyRequest>>();

                resultInfo = employeeService.SaveSubsidy(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        ///发放工资
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult PaymentSalary()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<int>>>();

                resultInfo = employeeService.PaymentSalary(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        ///撤销工资发放
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult RevokeSalary()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<int>>>();

                resultInfo = employeeService.RevokeSalary(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}